package com.doctor.windflower_doctor.db;

import java.io.File;

import android.R.bool;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.widget.Toast;

import com.doctor.windflower_doctor.util.Constent;

public class SharedPreferencesDB implements Constent {
	private SharedPreferences prefs = null;
	private Context context;
	private static SharedPreferencesDB sharedPreferencesDB = null;

	public static SharedPreferencesDB getInstance(Context context) {
		if (sharedPreferencesDB == null) {
			sharedPreferencesDB = new SharedPreferencesDB(context);
		}
		return sharedPreferencesDB;
	}

	public static void newInstance(Context context) {
		sharedPreferencesDB = new SharedPreferencesDB(context);
	}

	@SuppressWarnings("deprecation")
	protected SharedPreferencesDB(Context cxt) {
		this.context = cxt;
		Context context;
		try {
			context = cxt.createPackageContext(cxt.getPackageName(), Context.CONTEXT_IGNORE_SECURITY);
			prefs = context.getSharedPreferences(PREFS_FILE, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}

	}

	public void setIsLogin(boolean isLogin) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean("islogin", isLogin);
		editor.commit();
	}

	public boolean getIsLogin() {
		return prefs.getBoolean("islogin", false);
	}

	public void putImage(String img) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("img", img);
		editor.commit();
	}

	public String getImage() {
		return prefs.getString("img", null);
	}

	public String getEmail() {
		return prefs.getString("email", null);
	}

	public void setEmail(String email) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("email", email);
		editor.commit();
	}

	public void setAcceptQuestion(boolean istrue) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean("isaccept", istrue);
		editor.commit();
	}

	public boolean getIsAcceptQuestion() {
		return prefs.getBoolean("isaccept", false);
	}

	public void setPosition(String position) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("position", position);
		editor.commit();
	}

	public String getPosition() {
		return prefs.getString("position", null);
	}

	public void setSingleId(String docotorId) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("id", docotorId);
		editor.commit();
	}

	public String getSingleId() {
		return prefs.getString("id", null);
	}

	public void setCloseNum(String close) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("close", close);
		editor.commit();
	}

	public String getCloseNum() {
		return prefs.getString("close", "0");
	}

	public void setDocotorId(String docotorId) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("docotorId", docotorId);
		editor.commit();
	}

	public String getDocotorId() {
		return prefs.getString("docotorId", null);
	}

	public void setUserName(String userName) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("userName", userName);
		editor.commit();
	}

	public String getUserName() {
		return prefs.getString("userName", null);
	}

	public void setPassword(String password) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("password", password);
		editor.commit();
	}

	public String getPassword() {
		return prefs.getString("password", null);
	}

	public void setAccountName(String accountName) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("accountName", accountName);
		editor.commit();
	}

	public String getAccountName() {
		return prefs.getString("accountName", null);
	}

	public void setAssessToken(String token) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString("accessToken", token);
		editor.commit();
	}

	public String getAssessToken() {
		return prefs.getString("accessToken", null);
	}

	public void clear() {
		deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/shared_prefs"));// 删除share文件
		deleteFilesByDirectory(context.getCacheDir());// 清除本应用内部缓存

	}

	/**
	 * * 删除方法 这里只会删除某个文件夹下的文件，如果传入的directory是个文件，将不做处理 *
	 * 
	 * @param directory
	 */
	private static void deleteFilesByDirectory(File directory) {
		if (directory != null && directory.exists() && directory.isDirectory()) {
			for (File item : directory.listFiles()) {
				item.delete();
			}
		}
	}

	public boolean getIsFirst() {
		return prefs.getBoolean("isFirst", true);
	}

	public void setIsFirst(boolean isFirst) {
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean("isFirst", isFirst);
		editor.commit();
	}

}
